﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp17
{
    class Program
    {
       static Student student1 = new Student(6, "张三", 24);
       static Student student2 = new Student(1, "张四", 23);
       static Student student3 = new Student(3, "张一", 26);
       static List<Student> list = new List<Student>();
        static Hashtable ht = new Hashtable(); 
        static void Main(string[] args)
        {
            list.Add(student1);
            list.Add(student2);
            list.Add(student3);
            ht.Add(student1.nun, student1);
            ht.Add(student2.nun, student2);
            ht.Add(student3.nun, student3);
            hello();
        }
        public static void hello()
        {
            Console.WriteLine("1、添加学生信息。2、删除学生信息 3、查询学生信息。");
            int key = int.Parse(Console.ReadLine()) ;
            switch (key)
            {
                case 1:
                    Console.WriteLine("请输入学生的学号");
                    int num = int.Parse(Console.ReadLine());
                    Console.WriteLine("请输入学生的姓名");
                    string name = Console.ReadLine();
                    Console.WriteLine("请输入学生的年龄");
                    int age = int.Parse(Console.ReadLine());
                    Student student = new Student(num,name,age);                
                    if (ht.Contains(num))
                    {
                        Console.WriteLine("学号存在了不能重复输入");
                        hello();
                    }
                    else
                    {
                        list.Add(student);
                        ht.Add(student.nun, student);
                        Console.WriteLine("添加成功");
                        hello();
                        Console.ReadKey();
                    }
                  
                    break;
                case 2:
                    Console.WriteLine("请输入你要删除学生的学号");
                     num = int.Parse(Console.ReadLine());
                    foreach (var i in list)
                    {
                        if (ht.Contains(num))
                        {
                            list.Remove(i);
                            ht.Remove(i);
                            Console.WriteLine("删除成功");
                        }
                        else
                        {
                            Console.WriteLine("该学生不存在");
                            
                        }
                        hello();
                        Console.ReadKey();
                    }
                    break;
                case 3:
                    hello2();
                    break;
                default:
                    break;
            }
        }

        public static void hello2()
        {
            Console.WriteLine("1、查询所有（按学号排序）2、查询所有（按姓名排序），2、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");

            int key = int.Parse(Console.ReadLine());
            switch (key)
            {
                case 1:
                    Console.WriteLine("查询所有（按学号排序）");
                    list.Sort(new NumSort());
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    hello();
                    break;
                case 2:
                    Console.WriteLine("查询所有（按姓名排序）");
                    list.Sort(new NameSort());
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    hello();
                    break;
                case 3:
                    Console.WriteLine("查询所有（按年龄排序）");
                    list.Sort(new AgeSort());
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    hello();
                    break;
                case 4:
                    Console.WriteLine("请输入你要查询的学号");
                  int  nun = int.Parse(Console.ReadLine());
                    if (ht.Contains(nun))
                    {
                        Console.WriteLine("该学生存在");
                    }
                    else
                    {
                        Console.WriteLine("查无此学生");
                    }
                    break;
                case 5:
                    Environment.Exit(0);
                    break;
                default:
                    break;
            }
        }
    }
}
